<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use Gregwar\Captcha\CaptchaBuilder;
use DB;
use Gregwar\Captcha\PhraseBuilder;
use Captcha;
use Validator;
class BlogController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $res=DB::table('art')->orderBy('id','desc')->paginate(2);
        //显示首页
        return view('art.index',['art'=>$res]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //显示添加页面
        return view('art.add');
        // return view('layouts.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
        $data = $request->except('_token');
        $res = DB::table('art')->insert($data);
        if($res){
            return redirect('/blog');
        }else{
            return back();
        }


        // $messages = [
        //     'required'    => ':attribute 是必填字段',
        //     'size'    => ':attribute 前填写正确手机号格式',
        //     'email' => ':attribute 邮箱格式不正确',
        //     'min' => ':attribute 密码不能小于6位',
        // ];

        // $res =  Validator::make($request->all(), [
        //     'title' => 'required|max:30|min:8',
        //     'content' => 'required|min:20|max:255|unique:users',
        // ]);
        // dd($res->fails());
        
    

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $art = DB::table('art')->where('id',$id)->first();

        return view('art.show',['v'=>$art]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $art = DB::table('art')->where('id','=',$id)->first();
        return view('art.edit',['art'=>$art]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $res=DB::table('art')->where('id','=',$id)->update(['title'=>$request->title,'content'=>$request->content]);
        return redirect('/blog');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $res = DB::table('art')->where('id','=',$id)->delete();
        return redirect('/blog');
    }

    public function yulan(Request $request, $id)
    {
        return json_encode($request->only('title','content'));
    }

    public function getUser()
    {
        
        dd(Auth::user());
    }

    public function getCode()
    {
        // $phraseBuilder = new PhraseBuilder(6, '0123456789');
        // $builder = new CaptchaBuilder(null, $phraseBuilder);
        // $builder->build();
        // $content=$builder->output();
        // return response($content)->header('Content-Type','image/jpeg');
        return   captcha_src();
    }
}
